#pragma semicolon 1
#include <sourcemod>
#include <cstrike>
#include <sdktools>
#include <colors>
#include <PowerupsX>

#define PREFIX "{olive}PowerupsX {default}>{green} "

new AbilityRegen;

new Handle:g_RegenTimer[MAXPLAYERS + 1];
new Handle:g_ARegenTimer[MAXPLAYERS + 1];

public Plugin:myinfo= 
{
	name="PowerupsXL Regen",
	author="Skydive",
	description="Powerups X Regen Abilities",
	version="1.0",
	url=""
};

public OnPluginStart()
{
	NativeIncStart();
	
	AbilityRegen = PX_RegisterAbility("AbilityRegeneration");
	PX_SetAbilityName(AbilityRegen,"Regeneration");
	PX_SetAbilityDescription(AbilityRegen,"MaxHP+: Level*10.  Max.5: Regen Armor");
	PX_SetAbilityUsage(AbilityRegen,"Regen +1 Health Every .2 Seconds.");
	PX_AbilityUseColor(AbilityRegen,true);
	PX_SetAbilityColor(AbilityRegen,255,255,20,255);
	PX_SetAbilityMaxLevel(AbilityRegen,5);
	
	HookEvent("player_hurt",Event_PlayerHurt);
}

public PXF_OnAbilityGiven(Client, Ability)
{
	if(Ability == AbilityRegen)
	{
		if(g_RegenTimer[Client] == INVALID_HANDLE)
		{
			g_RegenTimer[Client] = CreateTimer(0.2, Regenerate, Client, TIMER_REPEAT);
		}
		if(g_RegenTimer[Client] == INVALID_HANDLE)
		{
			g_ARegenTimer[Client] = CreateTimer(0.2, ARegenerate, Client, TIMER_REPEAT);
		}
	}
}

public Action:Event_PlayerHurt(Handle:event, const String:name[],bool:dontBroadcast)
{
	new victim = GetClientOfUserId(GetEventInt(event, "userid"));
	if(PX_GetAbility(victim) == AbilityRegen)
	{
		if(g_RegenTimer[victim] == INVALID_HANDLE)
		{
			g_RegenTimer[victim] = CreateTimer(0.2, Regenerate, victim, TIMER_REPEAT);
		}
		if(g_ARegenTimer[victim] == INVALID_HANDLE)
		{
			g_ARegenTimer[victim] = CreateTimer(0.2, ARegenerate, victim, TIMER_REPEAT);
		}
	}
}

public Action:Regenerate(Handle:timer, any:Client)
{
	new Level = PX_GetLevel(Client, PX_GetAbility(Client));
	if(IsClientInGame(Client))
	{
		new ClientHealth = GetClientHealth(Client);
		
		if(PX_GetAbility(Client) == AbilityRegen)
		{
			if(ClientHealth < 100+Level*10)
			{
				SetClientHealth(Client, ClientHealth+Level);
			}
			else
			{
				SetClientHealth(Client, 100+Level*10);
				g_RegenTimer[Client] = INVALID_HANDLE;
				KillTimer(timer);
			}
		}
		return;
	}
	g_RegenTimer[Client] = INVALID_HANDLE;
	KillTimer(timer);
}

public Action:ARegenerate(Handle:timer, any:Client)
{
	new Level = PX_GetLevel(Client, PX_GetAbility(Client));
	if(IsClientInGame(Client) && Level >= 5)
	{
		new ClientArmor = GetClientArmor(Client);
		
		if(PX_GetAbility(Client) == AbilityRegen)
		{
			if(ClientArmor < 125)
			{
				SetClientArmor(Client, ClientArmor + 2);
			}
			else
			{
				SetClientArmor(Client, 125);
				g_ARegenTimer[Client] = INVALID_HANDLE;
				KillTimer(timer);
			}
		}
		return;
	}
	g_ARegenTimer[Client] = INVALID_HANDLE;
	KillTimer(timer);
}

stock SetClientHealth(client, amount)
{
	new HealthOffs = FindDataMapOffs(client, "m_iHealth");
	SetEntData(client, HealthOffs, amount, true);
}

stock SetClientArmor(client, amount)
{
	new ArmorOffs = FindDataMapOffs(client, "m_ArmorValue");
	SetEntData(client, ArmorOffs, amount, true);
}